<?php
error_reporting(E_ALL & ~E_NOTICE);
/*
    作者:Moid
    QQ:2993150260
    官网:http://zvccc.com
*/

class http_request
{
    public $header = [];
    public $server = [];
    public $get = [];
    public $post = [];
    public $cookie = [];
    public $files = [];
    public $rawContent = [];
    public $data;
    public $tmp;
    private $end = '1111222333444555aaaavaaa555444333222111';

    public function __construct($data)
    {

        $this->data = $data;
        $this->tmp = $data;

        if ($this->request($data) == 'false') {

            echo 'http_request:error on data' . PHP_EOL;
        }
    }


    public function request($data)
    {

        $eol = '\r';
        $eol2 = '\r\n';

        if ($data == null) {

            return  'false';
        }



        /*---------------------分析----------------------*/

        preg_match("/Host\:\s(.*?)\s/i", $data, $host);
        preg_match("/GET\s(.*?)\sHTTP\/1.1/i", $data, $get);
        preg_match("/POST\s(.*?)\sHTTP\/1.1/i", $data, $post);
        preg_match("/Referer\:\s(.*)\s/i", $data, $referer);
        preg_match("/Accept\:\s(.*)\s/i", $data, $accept);
        preg_match("/Content-Type\:\s(.*)\s/i", $data, $content_type);
        preg_match("/Content-Type\:\smultipart\/form-data\;\sboundary\=(.*)$eol2/i", $data, $content_type2);
        preg_match("/Cookie\:\s(.*)\s/i", $data, $cookie);
        preg_match("/Content-Length\:\s(.*)\s/i", $data, $content_length);

        /*---------------------分析----------------------*/




        /*--------------------header---------------------*/

        $this->header['host'] = $host[1];
        $array = explode(':', $host[1]);
        $this->header['ip'] = $array[0];
        $this->header['port'] = $array[1];
        $this->header['referer'] = $referer[1];
        $this->header['accept'] = @explode(',', $accept[1]);
        $this->header['content_type'] = $content_type[1];
        $this->header['content_type2'] = '--' . $content_type2[1];
        $this->header['content_length'] = $content_length[1];


        /*--------------------header---------------------*/





        /*---------------------GET/----------------------*/

        if ($get[1] != null) {

            $this->_GET_POST($get[1]);
        } else {

            $this->_GET_POST($post[1]);
        }

        /*---------------------GET-----------------------*/




        /*-------------------COOKIE---------------------*/

        $array = @explode(';', $cookie[1]);

        for ($i = 0; $i < count($array); $i++) {

            $data = @explode('=', $array[$i]);

            $this->cookie[$data[0]] = $data[1];
        }

        /*-------------------COOKIE---------------------*/






        /*-----------------POST/FILES-------------------*/

        $this->_FILES();

        /*-----------------POST/FILES-------------------*/
    }




    private function _FILES()
    {

        $eol = '\r';
        $eol2 = '\r\n';

        $this->tmp = substr_replace($this->tmp, '', strpos($this->tmp, $this->header['content_type']), mb_strlen($this->header['content_type'], 'utf-8'));

        $this->tmp = substr_replace($this->tmp, '', strpos($this->tmp, $this->header['content_type2'] . '--'), mb_strlen($this->header['content_type2'] . '--', 'utf-8'));

        if ($this->header['content_type2'] != null) {

            $list = explode($this->header['content_type2'], $this->tmp);

            $list[0] = null;

            for ($i = 1; $i < count($list); $i++) {

                @preg_match("/\sfilename\=\"(.*)\"\s/i", $list[$i], $filename);

                if ($filename[1] != null) {

                    @preg_match("/\sname\=\"(.*)\"\;/i", $list[$i], $name);

                    $l = explode('
', $list[$i]);

                    $file = null;

                    for ($ii = 4; $ii < count($l); $ii++) {

                        if ($i != count($l)) {

                            $file = $file . $l[$ii] . '
';
                        } else {

                            $file = $file . $l[$ii];
                        }
                    }

                    $this->files[$name[1]] = ['name' => $filename[1], 'data' => $file];
                } else {

                    @preg_match("/\sname\=\"(.*)\"$eol2 $eol2(.*)\s/i", $list[$i], $data);

                    $this->post[$data[1]] = $data[2];
                }
            }
        }
    }




    private function _GET_POST($value)
    {

        $e = @explode('?', $value);

        $array = @explode('&', $e[1]);

        for ($i = 0; $i < count($array); $i++) {

            $data = @explode('=', $array[$i]);

            $this->get[$data[0]] = $data[1];
        }
    }
}
